<?php
/*
+-----------------------------------------------------------------------------+
| $Id: *.php 2009-08-18 08:41:22Z Bleakwind $
| Copyright (c) 2003-2010 Bleakwind (www.weaverdream.com)
| http://www.weaverdream.com/
+-----------------------------------------------------------------------------+
*/

if (!defined( 'ENTRY_INDEX')){
    echo "<h1>Forbidden</h1><p>You don't have permission to access on this server.</p>";
    exit;
}

if( !preg_match("/^[0-9]*$/i",$sys->get['channel_id']) ){
    $sys->prompt("failed",$LANGUAGE['s']['channel']['channel_id_error']."<!-- channel_id_error -->");
} else {
    $sys->get['channel_id'] = (int)$sys->get['channel_id'];

    if ($sys->get['channel_id'] > 0) {
        $channel_info = $sys->return_channel_info($sys->get['channel_id']);

        if( !preg_match("/^[1-9][0-9]{0,10}$/",$channel_info['id']) ){
            $sys->prompt("failed",$LANGUAGE['s']['channel']['channel_id_not_exist']."<!-- channel_id not exist -->");
        } elseif ($channel_info['if_enable'] != "1") {
            $sys->prompt("failed",$LANGUAGE['s']['channel']['channel_id_not_enable']."<!-- channel_id not enable -->");
        } else {
            $sys->get['channel_id'] = $channel_info['id'];

            if ($ADMIN['ag_privilege']['topic'] == "1" || ($ADMIN['ag_privilege']['topic'] == "3" && $channel_info['channel_manage_admin_id'] != "")) {
                $ag_privilege_topic = "1";
            } else {
                $ag_privilege_topic = "2";
            }

            if ($ADMIN['ag_privilege']['channel_passinto'] == "1" || ($ADMIN['ag_privilege']['channel_passinto'] == "3" && $channel_info['channel_manage_admin_id'] != "")) {
                $ag_privilege_channel_passinto = "1";
            } else {
                $ag_privilege_channel_passinto = "2";
            }

            if ($ADMIN['ag_privilege']['channel_lockpost'] == "1" || ($ADMIN['ag_privilege']['channel_lockpost'] == "3" && $channel_info['channel_manage_admin_id'] != "")) {
                $ag_privilege_channel_lockpost = "1";
            } else {
                $ag_privilege_channel_lockpost = "2";
            }

            if ($ADMIN['ag_privilege']['channel_lockreply'] == "1" || ($ADMIN['ag_privilege']['channel_lockreply'] == "3" && $channel_info['channel_manage_admin_id'] != "")) {
                $ag_privilege_channel_lockreply = "1";
            } else {
                $ag_privilege_channel_lockreply = "2";
            }

            if ($ADMIN['ag_privilege']['channel_lockedit'] == "1" || ($ADMIN['ag_privilege']['channel_lockedit'] == "3" && $channel_info['channel_manage_admin_id'] != "")) {
                $ag_privilege_channel_lockedit = "1";
            } else {
                $ag_privilege_channel_lockedit = "2";
            }

            if ($ADMIN['ag_privilege']['channel_lockdel'] == "1" || ($ADMIN['ag_privilege']['channel_lockdel'] == "3" && $channel_info['channel_manage_admin_id'] != "")) {
                $ag_privilege_channel_lockdel = "1";
            } else {
                $ag_privilege_channel_lockdel = "2";
            }

            $sys->return_channel_current($channel_info['id']);

            if (!empty($channel_info['password'])) {
                $MEMBER['detail']['dialog'] = func::db_select(DB_TABLE_MEMBER_DETAIL, "dialog", "mid=".$MEMBER['id']);
                $MEMBER['detail']['dialog'] = unserialize($MEMBER['detail']['dialog'][0]['dialog']);
                $error_message = "";
                if ($sys->get['ope'] == "savepass") {
                    $sys->post['password'] = strtolower($sys->post['password']);
                    if( !preg_match("/^[0-9a-z_]+$/i",$sys->post['password']) ){
                        $error_message = $LANGUAGE['s']['channel']['password_error'];
                    } else {
                        if ($sys->post['password'] != strtolower($channel_info['password'])) {
                            $error_message = $LANGUAGE['s']['channel']['password_wrong'];
                        } else {
                            $MEMBER['detail']['dialog']['channel'][$channel_info['id']]['password'] = $sys->post['password'];
                            $sql_data = array(
                                "dialog" => "'".serialize($MEMBER['detail']['dialog'])."'",
                            );
                            $result = func::db_update(DB_TABLE_MEMBER_DETAIL, $sql_data, "mid='".$MEMBER['id']."'");
                            if (!$result) {
                                $error_message = $LANGUAGE['s']['channel']['password_save_error'];
                            }
                        }
                    }
                }
            }
            $need_pass = "2";
            if ( !empty($channel_info['password'])
                && ($MEMBER['detail']['dialog']['channel'][$channel_info['id']]['password'] != strtolower($channel_info['password']))
                && ($ag_privilege_topic != "1")
                && ($ag_privilege_channel_passinto != "1") ) {
                $need_pass = "1";
            } else {
                $need_pass = "2";

                $sys->channel_tree = array();
                $channel_tree   = $sys->return_channel_tree();
                $channel_tree[$sys->get['channel_id']]['disable'] = "1";

                if ($sys->get['ope'] == "move") {
                    if ($ag_privilege_topic != "1") {
                        $sys->prompt("failed",$LANGUAGE['s']['channel']['topic_move_privilege_error']."<!-- topic_move_privilege_error -->");
                    } elseif (!preg_match("/^[1-9][0-9]{0,10}$/",$sys->get['moveto'])) {
                        $sys->prompt("failed",$LANGUAGE['s']['channel']['topic_move_moveto_error']."<!-- topic_move_privilege_error -->");
                    } else {
                        $this_array = array();
                        if (!empty($sys->get['topic_id'])){
                            if (is_array($sys->get['topic_id'])) {
                                foreach ($sys->get['topic_id'] as $v) {
                                    if (preg_match("/^[1-9][0-9]{0,10}$/",$v)) {
                                        $this_array[$v] = $v;
                                    }
                                }
                            } else {
                                if (preg_match("/^[1-9][0-9]{0,10}$/",$sys->get['topic_id'])) {
                                    $this_array[$sys->get['topic_id']] = $sys->get['topic_id'];
                                }
                            }
                        }
                        if (!empty($sys->post['topic_id'])){
                            if (is_array($sys->post['topic_id'])) {
                                foreach ($sys->post['topic_id'] as $v) {
                                    if (preg_match("/^[1-9][0-9]{0,10}$/",$v)) {
                                        $this_array[$v] = $v;
                                    }
                                }
                            } else {
                                if (preg_match("/^[1-9][0-9]{0,10}$/",$sys->post['topic_id'])) {
                                    $this_array[$sys->post['topic_id']] = $sys->post['topic_id'];
                                }
                            }
                        }

                        if (!empty($sys->get['choice'])){
                            if (is_array($sys->get['choice'])) {
                                foreach ($sys->get['choice'] as $v) {
                                    if (preg_match("/^[1-9][0-9]{0,10}$/",$v)) {
                                        $this_array[$v] = $v;
                                    }
                                }
                            } else {
                                if (preg_match("/^[1-9][0-9]{0,10}$/",$sys->get['choice'])) {
                                    $this_array[$sys->get['choice']] = $sys->get['choice'];
                                }
                            }
                        }
                        if (!empty($sys->post['choice'])){
                            if (is_array($sys->post['choice'])) {
                                foreach ($sys->post['choice'] as $v) {
                                    if (preg_match("/^[1-9][0-9]{0,10}$/",$v)) {
                                        $this_array[$v] = $v;
                                    }
                                }
                            } else {
                                if (preg_match("/^[1-9][0-9]{0,10}$/",$sys->post['choice'])) {
                                    $this_array[$sys->post['choice']] = $sys->post['choice'];
                                }
                            }
                        }
                        if (!empty($this_array)) {
                            foreach ($this_array as $v) {
                                $topic_info = func::db_select(DB_TABLE_TOPIC, "*", "id=".$v);
                                $topic_info = $topic_info[0];

                                if (!preg_match("/^[1-9][0-9]{0,10}$/",$topic_info['id'])) {
                                    $sys->prompt("failed",$LANGUAGE['s']['channel']['topic_move_topic_not_exist']."<!-- topic_move_topic_not_exist -->");
                                } else {
                                    $sql_data = array(
                                        "channel_id" => $sys->get['moveto'],
                                    );
                                    $result = func::db_update(DB_TABLE_TOPIC, $sql_data, "id='".$v."'");
                                    if (!$result) {
                                        $sys->prompt("failed",$LANGUAGE['s']['channel']['topic_move_topic_error']."<!-- topic_move_topic_error -->");
                                    }
                                }
                                $sql_data = array(
                                    "number_total_topic"        => "number_total_topic-1",
                                    "number_total_post"         => "number_total_post-".($topic_info['number_reply']+1)."",
                                    "number_today_topic"        => "number_today_topic-1",
                                    "number_today_post"         => "number_today_post-".($topic_info['number_reply']+1)."",
                                );
                                $result = func::db_update(DB_TABLE_CHANNEL, $sql_data, "id=".(int)$topic_info['channel_id']."");
                                if (!$result) {
                                    $ajax_response->alert($LANGUAGE['s']['channel']['move_channel_number_error']);
                                }
                                $sql_data = array(
                                    "number_total_topic"        => "number_total_topic+1",
                                    "number_total_post"         => "number_total_post+".($topic_info['number_reply']+1)."",
                                    "number_today_topic"        => "number_today_topic+1",
                                    "number_today_post"         => "number_today_post+".($topic_info['number_reply']+1)."",
                                );
                                $result = func::db_update(DB_TABLE_CHANNEL, $sql_data, "id=".(int)$sys->get['moveto']."");
                                if (!$result) {
                                    $ajax_response->alert($LANGUAGE['s']['channel']['move_channel_number_error']);
                                }
                                if ( !$sys->update_channel_last((int)$topic_info['channel_id']) ) {
                                    $ajax_response->alert($LANGUAGE['s']['channel']['move_channel_last_error']);
                                }
                                if ( !$sys->update_channel_last((int)$sys->get['moveto']) ) {
                                    $ajax_response->alert($LANGUAGE['s']['channel']['move_channel_last_error']);
                                }
                            }
                        }
                        $sys->prompt("jump",$CONFIGURE['common']['control_index']."?act=channel&channel_id=".$sys->get['channel_id']."&pag=".$sys->get['pag']);
                    }
                }  elseif ($sys->get['ope'] == "del") {
                    if ($ag_privilege_topic != "1") {
                        $sys->prompt("failed",$LANGUAGE['s']['channel']['topic_del_privilege_error']."<!-- topic_del_privilege_error -->");
                    } else {
                        $this_array = array();
                        if (!empty($sys->get['topic_id'])){
                            if (is_array($sys->get['topic_id'])) {
                                foreach ($sys->get['topic_id'] as $v) {
                                    if (preg_match("/^[1-9][0-9]{0,10}$/",$v)) {
                                        $this_array[$v] = $v;
                                    }
                                }
                            } else {
                                if (preg_match("/^[1-9][0-9]{0,10}$/",$sys->get['topic_id'])) {
                                    $this_array[$sys->get['topic_id']] = $sys->get['topic_id'];
                                }
                            }
                        }
                        if (!empty($sys->post['topic_id'])){
                            if (is_array($sys->post['topic_id'])) {
                                foreach ($sys->post['topic_id'] as $v) {
                                    if (preg_match("/^[1-9][0-9]{0,10}$/",$v)) {
                                        $this_array[$v] = $v;
                                    }
                                }
                            } else {
                                if (preg_match("/^[1-9][0-9]{0,10}$/",$sys->post['topic_id'])) {
                                    $this_array[$sys->post['topic_id']] = $sys->post['topic_id'];
                                }
                            }
                        }

                        if (!empty($sys->get['choice'])){
                            if (is_array($sys->get['choice'])) {
                                foreach ($sys->get['choice'] as $v) {
                                    if (preg_match("/^[1-9][0-9]{0,10}$/",$v)) {
                                        $this_array[$v] = $v;
                                    }
                                }
                            } else {
                                if (preg_match("/^[1-9][0-9]{0,10}$/",$sys->get['choice'])) {
                                    $this_array[$sys->get['choice']] = $sys->get['choice'];
                                }
                            }
                        }
                        if (!empty($sys->post['choice'])){
                            if (is_array($sys->post['choice'])) {
                                foreach ($sys->post['choice'] as $v) {
                                    if (preg_match("/^[1-9][0-9]{0,10}$/",$v)) {
                                        $this_array[$v] = $v;
                                    }
                                }
                            } else {
                                if (preg_match("/^[1-9][0-9]{0,10}$/",$sys->post['choice'])) {
                                    $this_array[$sys->post['choice']] = $sys->post['choice'];
                                }
                            }
                        }
                        if (!empty($this_array)) {
                            foreach ($this_array as $v) {
                                $topic_info = func::db_select(DB_TABLE_TOPIC, "*", "id=".$v);
                                $topic_info = $topic_info[0];

                                if (!preg_match("/^[1-9][0-9]{0,10}$/",$topic_info['id'])) {
                                    $sys->prompt("failed",$LANGUAGE['s']['channel']['topic_del_topic_not_exist']."<!-- topic_del_topic_not_exist -->");
                                } else {

                                    $result = func::db_delete(DB_TABLE_TOPIC, "id=".$v."");
                                    if (!$result) {
                                        $sys->prompt("failed",$LANGUAGE['s']['channel']['topic_del_topic_error']."<!-- topic_del_topic_error -->");
                                    } else {
                                        $sql = "SELECT post_attach.*
                                                FROM ".DB_TABLE_POST_ATTACH." post_attach
                                                    LEFT JOIN ".DB_TABLE_POST." post ON post.id=post_attach.post_id
                                                WHERE post.topic_id=".$v."
                                                ORDER BY id desc";
                                        $result = &$db->Execute($sql);
                                        if (!$result) {
                                            echo $db->ErrorMsg();
                                        } else {
                                            while (!$result->EOF) {
                                                $filelist[] = array(
                                                    'id'        => $result->fields['id'],
                                                    'dir'       => $result->fields['dir'],
                                                    'filename'  => $result->fields['filename'],
                                                    'post_id'   => $result->fields['post_id'],
                                                    'admin_id'  => $result->fields['admin_id'],
                                                );
                                                $result->MoveNext();
                                            }
                                        }
                                        if (!empty($filelist)){
                                            foreach($filelist as $file){
                                                chmod($SETTING['dir_post_attach']."/".$file['dir']."/".$file['filename'], $SETTING['chmod_mode_file']);
                                                unlink($SETTING['dir_post_attach']."/".$file['dir']."/".$file['filename']);
                                            }
                                            $sql = "DELETE post_attach
                                                    FROM ".DB_TABLE_POST_ATTACH." post_attach
                                                        LEFT JOIN ".DB_TABLE_POST." post ON post.id=post_attach.post_id
                                                    WHERE post.topic_id=".$v;
                                            $result = &$db->Execute($sql);
                                            if (!$result) {
                                                $sys->prompt("failed",$LANGUAGE['s']['channel']['topic_del_attach_error']."<!-- topic_del_attach_error -->");
                                            }
                                        }
                                        $result = func::db_delete(DB_TABLE_TOPIC_TRANS, "topic_id=".$v."");
                                        if (!$result) {
                                            $sys->prompt("failed",$LANGUAGE['s']['channel']['topic_del_trans_error']."<!-- topic_del_trans_error -->");
                                        }
                                        $sql_data = array(
                                            "number_total_topic"        => "number_total_topic-1",
                                            "number_total_post"         => "number_total_post-".($topic_info['number_reply']+1)."",
                                            "number_today_topic"        => "number_today_topic-1",
                                            "number_today_post"         => "number_today_post-".($topic_info['number_reply']+1)."",
                                        );
                                        $result = func::db_update(DB_TABLE_CHANNEL, $sql_data, "id=".(int)$topic_info['channel_id']."");
                                        if (!$result) {
                                            $ajax_response->alert($LANGUAGE['s']['channel']['del_channel_number_error']);
                                        }
                                        if ( !$sys->update_channel_last((int)$topic_info['channel_id']) ) {
                                            $ajax_response->alert($LANGUAGE['s']['channel']['del_channel_last_error']);
                                        }
                                    }
                                }
                            }
                        }
                        $sys->prompt("jump",$CONFIGURE['common']['control_index']."?act=channel&channel_id=".$sys->get['channel_id']."&pag=".$sys->get['pag']);
                    }
                } else {
                    // Begin get topic
                    $record_pre     = (int)$CONFIG['page_record_topic_list'];
                    $total_record   = func::db_count_record(DB_TABLE_TOPIC, "channel_id=".$channel_info['id']);
                    $bwpage->set_record_listnum($record_pre);
                    $bwpage->set_record($total_record);
                    $bwpage->page($CONFIGURE['common']['control_index']."?act=channel&channel_id=".$channel_info['id']."&pag=%s");
                    $sql = "SELECT topic.*,
                                icon_topic.filename as icon_filename,
                                channel.id as channel_id, channel.name as channel_name
                            FROM ".DB_TABLE_TOPIC." topic
                                LEFT JOIN ".DB_TABLE_CHANNEL." channel ON channel.id=topic.channel_id
                                LEFT JOIN ".DB_TABLE_ICON_TOPIC." icon_topic ON icon_topic.id=topic.icon
                            WHERE channel_id=".$channel_info['id']." AND if_del!=1
                            ORDER BY topic.if_top, topic.rank DESC, topic.id DESC
                            LIMIT ".$bwpage->record_listfirst.",".$bwpage->record_listnum;
                    $result = &$db->Execute($sql);
                    if (!$result) {
                        echo $db->ErrorMsg();
                    } else {
                        while (!$result->EOF) {
                            $topic_list[] = array(
                                'id'                        => $result->fields['id'],
                                'channel_id'                => $result->fields['channel_id'],
                                'icon'                      => $result->fields['icon'],
                                'subject'                   => $result->fields['subject'],
                                'subject_len'               => mb_strwidth($result->fields['subject'],"UTF-8"),
                                'post_time'                 => $result->fields['post_time'],
                                'post_mid'                  => $result->fields['post_mid'],
                                'post_musername'            => $result->fields['post_musername'],
                                'post_ip'                   => $result->fields['post_ip'],
                                'post_pid'                  => $result->fields['post_pid'],
                                'number_reply'              => $result->fields['number_reply'],
                                'number_click'              => $result->fields['number_click'],
                                'last_time'                 => $result->fields['last_time'],
                                'last_mid'                  => $result->fields['last_mid'],
                                'last_musername'            => $result->fields['last_musername'],
                                'last_pid'                  => $result->fields['last_pid'],
                                'style_color'               => $result->fields['style_color'],
                                'style_fontstyle'           => $result->fields['style_fontstyle'],
                                'style_decoration'          => $result->fields['style_decoration'],
                                'style_begin'               => $result->fields['style_begin'],
                                'style_end'                 => $result->fields['style_end'],
                                'if_elite'                  => $result->fields['if_elite'],
                                'if_elite_begin'            => $result->fields['if_elite_begin'],
                                'if_elite_end'              => $result->fields['if_elite_end'],
                                'if_top'                    => $result->fields['if_top'],
                                'if_top_begin'              => $result->fields['if_top_begin'],
                                'if_top_end'                => $result->fields['if_top_end'],
                                'if_close'                  => $result->fields['if_close'],
                                'if_close_begin'            => $result->fields['if_close_begin'],
                                'if_close_end'              => $result->fields['if_close_end'],
                                'if_move'                   => $result->fields['if_move'],
                                'if_move_id'                => $result->fields['if_move_id'],
                                'if_del'                    => $result->fields['if_del'],
                                'rank'                      => $result->fields['rank'],

                                'icon_filename'             => $result->fields['icon_filename'],

                                'channel_id'                => $result->fields['channel_id'],
                                'channel_name'              => $result->fields['channel_name'],
                                'channel_name_len'          => mb_strwidth($result->fields['channel_name'],"UTF-8"),
                            );
                            $result->MoveNext();
                        }
                    }
                }
            }
        }
    }
    $t->assign(array(
        "channel_id"                    => $sys->get['channel_id'],
        "channel_info"                  => $channel_info,
        "channel_current"               => $sys->channel_current,
        "channel_tree"                  => $channel_tree,
        "need_pass"                     => $need_pass,
        "error_message"                 => $error_message,
        "topic_list"                    => $topic_list,
        "pagination"                    => $bwpage->pagination,
    ));
}
if (!empty($sys->channel_current)) { 
    foreach($sys->channel_current as $v){ 
        $public_var['page_place'][] = $v['name']; 
        $public_var['page_keyword'] = array_merge((array)$public_var['page_keyword'], array_filter(preg_split("/[\s,]+/", $v['keyword'])));
    }
}
?>
